﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RichTextBoxWithIntellisense
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<String> list = null;

        public List<String> ContentAssistSource
        {
            get { return (List<String>)GetValue(ContentAssistSourceProperty); }
            set { SetValue(ContentAssistSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ContentAssisteSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentAssistSourceProperty =
            DependencyProperty.Register("ContentAssistSource", typeof(List<String>), typeof(MainWindow), new UIPropertyMetadata(new List<string>()));


        public List<char> ContentAssistTriggers
        {
            get { return (List<char>)GetValue(ContentAssistTriggersProperty); }
            set { SetValue(ContentAssistTriggersProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ContentAssistTriggers.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentAssistTriggersProperty =
            DependencyProperty.Register("ContentAssistTriggers", typeof(List<char>), typeof(MainWindow), new UIPropertyMetadata(new List<char>()));


        public MainWindow()
        {
            InitializeComponent();

            InitRichTextBoxSource();
            this.ContentAssistSource = this.list;
            InitRichTextBoxIntellisenseTrigger();
            richTextBoxEx1.Focus();
            DataContext = this;
            this.richTextBoxEx1.FilterItems += richTextBoxEx1_FilterItems;
        }

        void richTextBoxEx1_FilterItems(object sender, string filterStr)
        {
            IEnumerable<string> temp = this.list.Where(s => s.ToUpper().StartsWith(filterStr.ToUpper()));
            this.ContentAssistSource = temp.ToList();
        }

        private void InitRichTextBoxIntellisenseTrigger()
        {
            ContentAssistTriggers.Add('@');
            ContentAssistTriggers.Add('.');
        }

        private void InitRichTextBoxSource()
        {
            this.list = new List<string>();
            this.list.Add("aaal");
            this.list.Add("as");
            this.list.Add("aacp");
            this.list.Add("aid");
            this.list.Add("asap");
            this.list.Add("boy");
            this.list.Add("big");
            this.list.Add("before");
            this.list.Add("belong");
            this.list.Add("can");
            this.list.Add("clever");
            this.list.Add("cool");
            this.list.Add("data");
            this.list.Add("delete");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TextRange textRange = new TextRange(richTextBoxEx1.Document.ContentStart, richTextBoxEx1.Document.ContentEnd);
            string str = textRange.Text.Replace(System.Environment.NewLine, string.Empty);
            MessageBox.Show(textRange.Text);
        }
    }
}